package gepes.servlet;

import gepes.exepcion.DAOExcepcion;
import gepes.modelo.Usuario;
import gepes.negocio.GestionUsuario;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class RegistroUsuarioServlet
 */
@WebServlet("/RegistroUsuarioServlet")
public class RegistroUsuarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistroUsuarioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int Login = 0;
		String nombre = request.getParameter("nombre");
		String apepaterno = request.getParameter("apellidopaterno");
		String apematerno = request.getParameter("apellidomaterno");
		String correo = request.getParameter("email");
		String clave = request.getParameter("contrasena");

		/*Usuario us =new Usuario();
		us.setNombre(nombre);
		us.setApepaterno(apepaterno);
		us.setApematerno(apematerno);
		us.setCorreo(correo);
		us.setClave(clave);*/
		
		GestionUsuario negocio = new GestionUsuario();

		try {
			Usuario vo = negocio.insertar(nombre, apepaterno, apematerno, correo, clave);
			
			HttpSession session = request.getSession();
			session.setAttribute("USUARIO_ACTUAL", vo);
			
			HttpSession sessionMi = request.getSession();
			sessionMi.setAttribute("msg", "Registro satisfactorio");
			sessionMi.setAttribute("pagina", "login.jsp");
			
			
			RequestDispatcher rd = request
					.getRequestDispatcher("mensaje.jsp");
			rd.forward(request, response);
			return;
		} catch (DAOExcepcion e) {
			request.setAttribute("MENSAJE", "Mensaje: " + e.getMessage());	
		/*} catch (LoginExcepcion e) {			
			request.setAttribute("MENSAJE", "Usuario y/o clave incorrectos");
		}*/

		RequestDispatcher rd = request.getRequestDispatcher("/RegistroUsuario.jsp");
		rd.forward(request, response);
	}
	}

}
